const GROUP_NAME = 'policies';
const {
  getPolicies,
  getPolicie,
  addPolicy,
  deletePolicyById,
  updatePolicy,
  getGroups,
  getPoliciesLogined,
} = require('../handler/policiesHandler');

exports.plugin = {
  name: 'policiesRouter',
  register(server) {
    const { Joi, headers } = server;

    // 获取登录用户权限
    const getPoliciesLoginedRoute = {
      method: 'get',
      path: `/${GROUP_NAME}/logined`,
      handler: getPoliciesLogined,
      config: {
        notes: '获取登录用户权限',
        description: '获取登录用户权限',
        tags: ['api', GROUP_NAME],
        plugins: {
          'hapi-swagger': {
            payloadType: 'form',
          },
        },
        validate: {
          headers,
        },
      },
    };

    // 获取策略
    const getPoliciesRoute = {
      method: 'get',
      path: `/${GROUP_NAME}`,
      handler: getPolicies,
      config: {
        notes: '获取所有策略',
        description: '获取所有策略',
        tags: ['api', GROUP_NAME],
        plugins: {
          'hapi-swagger': {
            payloadType: 'form',
          },
        },
        validate: {
          headers,
          query: Joi.object({
            page: Joi.number()
              .integer()
              .min(1)
              .default(1)
              .description('页码：默认第1页，最小值为1')
              .error(() => new Error('页码：默认第1页，最小值为1')),
            perPage: Joi.number()
              .integer()
              .min(1)
              .default(10)
              .description('每页的记录数：默认10条，最小值为1')
              .error(() => new Error('每页的记录数：默认10条，最小值为1')),
            searchField: Joi.string()
              .allow('method', 'path', 'group', 'effect')
              .default('')
              .description('要搜索的字段')
              .error(() => new Error('非法字段')),
            searchValue: Joi.string()
              .default('')
              .description('要搜索的内容')
              .error(() => new Error('输入内容')),
          }),
        },
      },
    };

    // 根据id获取策略
    const getPolicieRoute = {
      method: 'get',
      path: `/${GROUP_NAME}/{id}`,
      handler: getPolicie,
      config: {
        notes: '根据id获取策略',
        description: '根据id获取策略',
        tags: ['api', GROUP_NAME],
        plugins: {
          'hapi-swagger': {
            payloadType: 'form',
          },
        },
        validate: {
          headers,
          params: Joi.object({
            id: Joi.string().required().hex().length(24)
              .description('用户id'),
          }),
        },
      },
    };


    // 添加策略
    const addPolicyRoute = {
      method: 'post',
      path: `/${GROUP_NAME}`,
      handler: addPolicy,
      config: {
        notes: '添加策略',
        description: '添加策略',
        tags: ['api', GROUP_NAME],
        plugins: {
          'hapi-swagger': {
            payloadType: 'form',
          },
        },
        validate: {
          headers,
          payload: Joi.object({
            path: Joi.string().required().description('路由'),
            desc: Joi.string().required().description('路由描述'),
            method: Joi.string()
              .required()
              .allow('get', 'post', 'delete', 'put')
              .description('请求方法'),
            group: Joi.string()
              .required()
              .min(2)
              .max(18)
              .description('所属组名:2-18个字符')
              .error(() => new Error('所属组名:2-18个字符')),
            // effect: Joi.string().required().allow('permit', 'deny')
            // .description('效果:permit-允许、deny-拒绝'),
          }),
        },
      },
    };

    // 根据ID更新策略
    const updatePolicyRoute = {
      method: 'put',
      path: `/${GROUP_NAME}/{id}`,
      handler: updatePolicy,
      config: {
        notes: '根据ID更新策略',
        description: '根据ID更新策略',
        tags: ['api', GROUP_NAME],
        plugins: {
          'hapi-swagger': {
            payloadType: 'form',
          },
        },
        validate: {
          headers,
          params: Joi.object({
            id: Joi.string().required().hex().length(24)
              .description('用户id'),
          }),
          payload: Joi.object({
            path: Joi.string().regex(/\/.+/).description('路由'),
            desc: Joi.string().description('路由描述'),
            method: Joi.string()
              .allow('get', 'post', 'delete', 'put')
              .description('请求方法')
              .error(() => new Error('错误的请求方法')),
            group: Joi.string()
              .allow('users', 'classes', 'questions')
              .description('所属组名')
              .error(() => new Error('错误的组名')),
            // effect: Joi.string().allow('permit', 'deny').description('效果:permit-允许、deny-拒绝'),
          }),
        },
      },
    };

    // 通过ID删除策略
    const deletePolicyRoute = {
      method: 'delete',
      path: `/${GROUP_NAME}/{id}`,
      handler: deletePolicyById,
      config: {
        notes: '通过ID删除策略',
        description: '通过ID删除策略',
        tags: ['api', GROUP_NAME],
        validate: {
          headers,
          params: Joi.object({
            id: Joi.string()
              .required()
              .hex()
              .length(24)
              .description('用户id')
              .error(() => new Error('用户id不合法')),
          }),
        },
      },
    };

    // 获取所有的组
    const getGroupsRoute = {
      method: 'get',
      path: `/${GROUP_NAME}/groups`,
      handler: getGroups,
      config: {
        notes: '获取所有的组',
        description: '获取所有的组',
        tags: ['api', GROUP_NAME],
        validate: {
          headers,
        },
      },
    };

    server.route([
      getPoliciesRoute,
      getPolicieRoute,
      addPolicyRoute,
      deletePolicyRoute,
      updatePolicyRoute,
      getGroupsRoute,
      getPoliciesLoginedRoute,
    ]);
  },
};
